package cn.topevery.garden.wapi.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.topevery.garden.dal.dao.impl.PointDaoImpl;
import cn.topevery.garden.dal.logic.PointFileLogic;
import cn.topevery.garden.dal.logic.PointLogic;
import cn.topevery.garden.entity.dto.PointDTO;
import cn.topevery.garden.entity.po.Point;
import cn.topevery.garden.entity.po.PointFile;
import cn.topevery.garden.entity.query.PointQuery;
import cn.topevery.garden.entity.vo.PointVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.enums.GeometryTypeEnum;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Api(tags = "样点管理表")
@RestController
@RequestMapping("/point")
public class PointController extends BaseController {
    @Autowired
    private Mapper mapper;

    @Autowired
    PointLogic pointLogic;

    @Autowired
    PointDaoImpl pointDao;

    @Autowired
    private PointFileLogic pointFileLogic;

    @ApiOperation(value = "获取样点管理表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<PointVO>> listPage(@RequestBody PointQuery query) {
        DataPager<PointVO> page = pointLogic.listPage(query);
        for (PointVO pointVO : page.records) {
            if (pointVO.getMapRanges() != null) {
                pointVO.setMapRangesPoints(GeometryHelper.getGeometryPoint(pointVO.getMapRanges()));
            }
            List<PointFile> list = pointFileLogic.getListByPid(pointVO.getId());
            pointVO.setFileList(list);
        }
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 样点管理表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody PointDTO dto) {
        Point point = mapper.map(dto, Point.class);
        if (dto.getId() == null) {
            point.setPointId(UUID.randomUUID().toString());
            point.setDbStatus(false);
        }
        point.setStatus(0);
        point.setUpdateTime(LocalDateTime.now());
        if (CollectionUtils.isNotEmpty(dto.getMapRangesPoints())) {
            point.setMapRanges(GeometryHelper.getGeometry(GeometryHelper.getWellKnownText(dto.getMapRangesPoints(), GeometryTypeEnum.PLANE)));//百度坐标
            point.setGeomMkt(GeometryHelper.getGeometry(GeometryHelper.getMercatorWellKnownText(dto.getMapRangesPoints(), GeometryTypeEnum.PLANE)));//百度魔卡托坐标
            point.setGeoWkt(GeometryHelper.getWellKnownText(dto.getMapRangesPoints(), GeometryTypeEnum.PLANE));
        }
        String userId = getUser().id.toString();
        pointLogic.saveOrUpdate(userId, point);
        pointFileLogic.save(userId, point.getId(), point.getFileList());
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<PointVO> getInfoById(@RequestParam Integer id) {
        Point entity = pointLogic.getEntity(id);
        List<PointFile> fileList = pointFileLogic.getListByMainId(id);
        PointVO pointVO = new PointVO();
        BeanUtil.copyProperties(entity, pointVO, "mapRanges", "geomMkt");
        pointVO.setFileList(fileList);
        if (entity.getMapRanges() != null) {
            pointVO.setMapRangesPoints(GeometryHelper.getGeometryPoint(entity.getMapRanges()));
        }
        return JsonResult.ok(pointVO);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        pointLogic.deleteBatchById(getUser().id.toString(), ids);
        pointFileLogic.deleteByMainId(ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出违规记录表信息")
    @GetMapping("/export")
    public void export(PointQuery query, HttpServletResponse response) {
        List<PointVO> list = pointLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "样点管理表", "sheet1", PointVO.class, "样点管理表", response);
    }

    @ApiOperation(value = "获取样点管理表页列表")
    @PostMapping("/getBySectionId")
    public JsonResult<DataPager<PointVO>> getBySectionId(@RequestBody PointQuery query) {
        return JsonResult.ok(pointDao.getBySectionId(query));
    }

    @ApiOperation(value = "根据标段id获取可调入(状态 0)/调出(状态 1)对应样点")
    @PostMapping("/getPointListBySectionId")
    public JsonResult<DataPager<PointVO>> getPointListBySectionId(@RequestBody PointQuery query) {
        return JsonResult.ok(pointLogic.getPointListBySectionId(query));
    }

    @ApiOperation(value = "设定样点的调入调出状态 调入状态 0 调出 1 调入")
    @PostMapping("/setPointStatus")
    public JsonResult<Boolean> setPointStatus(@RequestBody PointDTO dto) {
        pointLogic.setPointStatus(dto.getIdList(), dto.getStatus());
        return JsonResult.ok(true);
    }

    @ApiOperation(value = "根据标段id获取样点")
    @GetMapping("/getListBySectionId")
    public JsonResult<List> getListBySectionId(@RequestParam Integer sectionId) {
        return JsonResult.ok(pointLogic.getListBySectionId(sectionId));
    }
}
